﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using KerriganDataContract;
using System.Web.Security;
using KerriganDataContract.Blog;
using KerriganDataContract.Direction;
using KerriganDataContract.Person;

namespace KerriganBlog
{
    public partial class EditArticle : System.Web.UI.Page
    {


        private string ACTIVITY
        {
            get
            {
                if (ViewState["Activity"] == null)
                    ViewState["Activity"] = null;
                return (string)ViewState["Activity"];
            }
            set
            {
                ViewState["Activity"] = value;
            }
        }
           private int ID
           {
               get
               {
                   if (ViewState["ID"] == null)
                       ViewState["ID"] = -1;
                   return (int) ViewState["ID"];
               }
               set
               {
                   ViewState["ID"] = value;
               }
           }


        protected void Page_Load(object sender, EventArgs e)
        {

            if (!IsPostBack)
            {
                ID = Convert.ToInt32(Request.QueryString["ID"]);
                ACTIVITY = Request.QueryString["Activity"];

                    LoadExpanse();
  
            }

        }


           private void LoadExpanse()
           {
               using (ServiceProxy Srv = new ServiceProxy())
               {
                   BlogDC res = Srv.Service.GetAritcleById(ID);
                   if (ACTIVITY == "edit")
                   {
                       txtTitle.Text = res.Title;
                       TxtBoxContent.Value = res.ContentTopic;
                       DropDownListCategory.DataSource = Srv.Service.GetCategoryToDisplay();
                       DropDownListCategory.SelectedValue = res.BlogCategory.IdBlogCategory.ToString();
                       DropDownListCategory.DataBind();
                   }
                   else {

                       DropDownListCategory.DataSource = Srv.Service.GetCategoryToDisplay();
                       DropDownListCategory.DataBind();
                   }

               }

           }



           protected void btnOkay_Click(object sender, EventArgs e)
           {
               try
               {
                   if(ACTIVITY =="edit"){
                  UpdateExpanse();
                  ClientScript.RegisterStartupScript(this.GetType(), "onload", "onSuccess();", true);
                   }else{
                       AddArticle();
                       ClientScript.RegisterStartupScript(this.GetType(), "onload", "onSuccess();", true);
                   }
              }
              catch(Exception ex)
              {
                  ClientScript.RegisterStartupScript(this.GetType(), "onload", "onError();", true);
              }
          }
  
          private void UpdateExpanse()
          {
              using (ServiceProxy Srv = new ServiceProxy())
              {
                  BlogDC bo = new BlogDC();
                  bo.BlogCategory = new BlogCategoryDC();
                  bo.BlogCategory.IdBlogCategory = int.Parse(DropDownListCategory.SelectedValue);
                  bo.Title = txtTitle.Text;
                  bo.ContentTopic = TxtBoxContent.InnerText;
                  Srv.Service.UpdateArticle(ID,bo);

              }

          }

          private void AddArticle()
          {
              using (ServiceProxy Srv = new ServiceProxy())
              {
                  string requestCookie = Request.Cookies["AXPXAUTHFWC"].Value;
                  FormsAuthenticationTicket authentification = FormsAuthentication.Decrypt(requestCookie);
                  string user = authentification.UserData;
                  string[] idUser = user.Split(',');

                  DirectionEmployeeDC emp = new DirectionEmployeeDC();
                  emp.Person = new PersonDC();
                  emp.Person.IdPerson = int.Parse(idUser[2]);
                  BlogDC bo = new BlogDC();
                  bo.BlogCategory = new BlogCategoryDC();
                  bo.BlogCategory.IdBlogCategory = int.Parse(DropDownListCategory.SelectedValue);
                  bo.Person = new PersonDC();
                  bo.Person.IdPerson = int.Parse(idUser[2]);
                  bo.Title = txtTitle.Text;
                  bo.ContentTopic = TxtBoxContent.InnerText;
                  bo.PostedBy = emp;
                  Srv.Service.AddArticle(bo);

              }

          }



    }
}